//
// Created by guanyicheng on 2022/3/14.
//

#include "../../common.h"

class Solution {
public:
    vector<vector<int>> threeSum(vector<int> &nums) {

        if (nums.size() < 1) {
            return {};
        }
        sort(nums.begin(), nums.end());

        vector<vector<int>> res{};
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] > 0) {
                return res;
            }
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }

            int left = i + 1, right = nums.size() - 1;
            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum == 0) {
                    res.push_back({nums[i], nums[left], nums[right]});
                    while (left < right && nums[right] == nums[right - 1]) --right;
                    while (left < right && nums[left] == nums[left + 1]) ++left;
                    ++left;
                    --right;
                } else if (sum < 0) {
                    ++left;
                } else {
                    --right;
                }
            }
        }
        return res;
    }
};

int main() {
    vector<int> v{0};
    cout << Solution().threeSum(v).size() << endl;
}